package fr.expdev.bndd.data.sqlite;

import fr.expdev.bndd.data.Configuration;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BNDDSQLite extends SQLiteOpenHelper {
	
	/**
	 * Numéro de la version en cours
	 */
	private static final int DATABASE_VERSION = 1;

	/**
	 * Tables de la base de données
	 */
	public enum Table {
		POSTS("posts"),
		CONFIG("config");
		
		private String tableName;
		
		private Table(String c) {
			tableName = c;
		}

		@Override
		public String toString() {
			return tableName;
		}
	}
	
	/**
	 * Colonnes de la table des posts
	 */
	public enum PostColumn {
		POST_ID(0),
		POST_TITLE(1),
		POST_PUBLICATIONDATE(2),
		POST_EXCERPT(3),
		POST_LINK(4),
		POST_TYPE(5),
		POST_FAVORITE(6);
		
		private int id;
		
		private PostColumn(int x) {
			id = x;
		}
		
		public int getId() {
			return id;
		}
	}
	
	/**
	 * Colonnes de la table des posts
	 */
	public enum ConfigColumn {
		CONFIG_NAME(0),
		CONFIG_VALUE(1);
		
		private int id;
		
		private ConfigColumn(int x) {
			id = x;
		}
		
		public int getId() {
			return id;
		}
	}

	public BNDDSQLite(Context context) {
		super(context, Table.POSTS.toString(), null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(
				"CREATE TABLE " + Table.POSTS + "(" +
					PostColumn.POST_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
					PostColumn.POST_TITLE + " TEXT NOT NULL, " +
					PostColumn.POST_PUBLICATIONDATE + " TEXT NOT NULL, " +
					PostColumn.POST_EXCERPT + " TEXT NOT NULL, " +
					PostColumn.POST_LINK + " TEXT NOT NULL, " +
					PostColumn.POST_TYPE + " TEXT NOT NULL," +
					PostColumn.POST_FAVORITE + " INTEGER " +
				");");
		
		db.execSQL(
				"CREATE TABLE " + Table.CONFIG+ "(" +
					ConfigColumn.CONFIG_NAME + " INTEGER PRIMARY KEY, " +
					ConfigColumn.CONFIG_VALUE + " TEXT " +
				");");
		insertConfiguration(db);
	}
	
	/**
	 * Insertion de la configration de base
	 * @param db
	 */
	private void insertConfiguration(SQLiteDatabase db) {
		
		ContentValues values = new ContentValues();
		
		values.put(ConfigColumn.CONFIG_NAME.toString(), Configuration.Key.AUTOREFRESH_ENABLED.intValue());
		values.put(ConfigColumn.CONFIG_VALUE.toString() , "true");
		db.insert(Table.CONFIG.toString(), null, values);
		
		values.put(ConfigColumn.CONFIG_NAME.toString(), Configuration.Key.AUTOREFRESH_INTERVAL.intValue());
		values.put(ConfigColumn.CONFIG_VALUE.toString() , "30");
		db.insert(Table.CONFIG.toString(), null, values);
		
		values.put(ConfigColumn.CONFIG_NAME.toString(), Configuration.Key.REFRESH_ATSTARTUP.intValue());
		values.put(ConfigColumn.CONFIG_VALUE.toString() , "true");
		db.insert(Table.CONFIG.toString(), null, values);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onCreate(db);
	}

}
